﻿@{
    ViewBag.Title = "用户列表";
    ViewBag.PathToBtn = "/Consumer/List";
    ViewBag.TextBtn = "刷新";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                用户列表
            </div>
            <div class="panel-body">
                <div class="dataTable_wrapper">
                    <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
                        <thead>
                            <tr>
                                <th>客户编号</th>
                                <th>昵称</th>
                                <th>电子水票数量</th>
                                <th>性别</th>
                                <th>注册时间</th>
                                <th>所属店铺/默认访问</th>
                                <th>绑定店员</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>                           
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="ChangeShopModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 500px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">更改用户绑定店铺（<font color="red">仅显示已激活的店铺</font>）</h4>
            </div>
            <div class="modal-body">
                <div style="margin-bottom: 5px;">
                    <input type="text" class="form-control" placeholder="店铺名称" style="width: 409px;display: inline-block" id="shopKeyWords" />
                    <a class="btn btn-primary" onclick="searchShops();">查询</a>
                </div>
                <div class="table-responsive table-bordered" style="max-height: 250px;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>店铺名称</th>
                                <th style="text-align: center">操作</th>
                            </tr>
                        </thead>
                        <tbody id="shopsData"></tbody>
                    </table>
                    <input type="hidden" id="hidConsumerId" />
                    <input type="hidden" id="hidIsSetDefaultShop" />
                </div>
            </div>
        </div>
    </div>
</div>

@section javascript{
    <script src="/Content/admin/dist/js/pagingHelper.js"> </script>
    <script type="text/javascript">
        var dataParams = {};
        var serverProcessPath = "/Consumer/Listing";
        var showColumns = [
            { data: "Id" },
            { data: "NickName" },
            { data: "CouponCount" },
            { data: "Sex" },
            { data: "RegTime" },
            { data: "ShopName" },
            { data: "ShopAccountDisplayName" },
            { data: "Action" }
        ];
        pagingHelper.Paging(serverProcessPath, dataParams, showColumns, null, false);

        var isSearched = false;

        //isSetDefaultShop（0否   1是）
        function changeShop(id, isSetDefaultShop) {
            $("#hidConsumerId").val(id);
            $("#hidIsSetDefaultShop").val(isSetDefaultShop);
            if (!isSearched) {
                searchShops();
            }
        }

        function confirmChangeShop(shopId) {
            var isSetDefaultShop = $("#hidIsSetDefaultShop").val();
            var confirmTip = "";
            var successTip = "";
            if (isSetDefaultShop == "0") {
                confirmTip = "确定将此用户绑定到此店铺吗？";
                successTip = "绑定成功";
            } else {
                confirmTip = "确定将此店铺设置为用户的默认访问店铺吗？";
                successTip = "设置成功";
            }
            alertify.okBtn("确定").cancelBtn("取消").confirm(confirmTip, function () {
                $.post("@Url.Action("ChangeShop")", "consumerId=" + $("#hidConsumerId").val() + "&shopId=" + shopId + "&isSetDefaultShop=" + isSetDefaultShop, function (data) {
                    if (data.Status) {
                        $("#ChangeShopModal").modal("hide");
                        reloadList();
                        alertify.success(successTip);
                    } else {
                        alertify.alert(data.Message);
                    }
                });
            });
        }

        function unbundShop(consumerId) {
            alertify.okBtn("确定").cancelBtn("取消").confirm("确定解绑吗？", function() {
                $.post("@Url.Action("ChangeShop")", "consumerId=" + consumerId + "&shopId=0", function(data) {
                    if (data.Status) {
                        reloadList();
                        alertify.success("解绑成功");
                    } else {
                        alertify.alert(data.Message);
                    }
                });
            });
        }

        function searchShops() {
            isSearched = true;
            $("#shopsData").html("");
            $.post("@Url.Action("GetActiveShopList", "Shops")", "keyWords=" + encodeURIComponent($("#shopKeyWords").val()), function(data) {
                if (data != null) {
                    for (var i = 0; i < data.length; i++) {
                        $("#shopsData").append("<tr><td>" + data[i].Name + "</td><td align='center'><a class='btn btn-info' onclick='confirmChangeShop(" + data[i].Id + ");'>绑定</a></td></tr>");
                    }
                }
            });
        }

        function reloadList() {
            pagingHelper.reloadWithDiffPath("@Url.Action("Listing")");
        }
    </script>
}